<?php

/**
* 
*/
class GetAccessToken{

	//存储公众号的AppID和AppSecret
	private $appID = 'wxef00bbf8e06b0555';
	private $appSecret = '5fb9bc7d169d3e075149d608ac4975f6';

	
	//请求token并存到缓存文件中
	public function cacheToken()
	{
		//准备好url
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='. $this -> appID .'&secret=' . $this -> appSecret;
		//curl请求信息
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($ch);
		if (curl_errno($ch)) {
			var_dump(curl_error($ch));
		}
		curl_close($ch);
		//将token提取出来
		$token = json_decode($result,true)['access_token'];
		file_put_contents('public/access_token.txt',$token);
		// echo $token;
	}
	//读取token
	public static function getToken()
	{
		$content = file_get_contents('public/access_token.txt');
		return $content;
	}
}